<template>
  <div class="auth-container">
    <form class="auth-form" @submit.prevent="handleSubmit">
      <h2>用户注册</h2>

      <!-- <div class="form-group">
        <label>用户名</label>
        <input
          type="text"
          v-model="form.username"
          required
          placeholder="请输入用户名"
        >
      </div> -->

      <div class="form-group">
        <label>邮箱</label>
        <input
          type="email"
          v-model="form.email"
          required
          placeholder="请输入有效邮箱"
        >
      </div>

      <div class="form-group">
        <label>密码</label>
        <input
          type="password"
          v-model="form.password"
          required
          placeholder="至少6位密码"
          minlength="6"
        >
      </div>

      <div class="form-group">
        <label>确认密码</label>
        <input
          type="password"
          v-model="form.confirmPassword"
          required
          placeholder="请再次输入密码"
          :pattern="form.password"
        >
      </div>

      <button type="submit" :disabled="isSubmitting">
        {{ isSubmitting ? '注册中...' : '立即注册' }}
      </button>

      <div class="auth-links">
        <router-link to="/login">已有账号？立即登录</router-link>
      </div>

      <div v-if="errorMessage" class="error-message">
        {{ errorMessage }}
      </div>
    </form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: '',
        email: '',
        password: '',
        confirmPassword: ''
      },
      isSubmitting: false,
      errorMessage: ''
    }
  },
  methods: {
    validateForm() {
      if (this.form.password !== this.form.confirmPassword) {
        this.errorMessage = '两次输入的密码不一致'
        return false
      }
      this.errorMessage = ''
      return true
    },

    async handleSubmit() {
      if (!this.validateForm()) return
      
      try {
        this.isSubmitting = true
        // 创建包含邮箱和密码的 JSON 数据
        const userData = {
          email: this.form.email,
          password: this.form.password
        }
        
        // 将用户数据转换为 JSON
        const jsonData = JSON.stringify(userData)
        console.log('JSON 数据:', jsonData)
        
        // 发送数据到服务器
        const response = await fetch('http://8.134.212.44:9999/user/register', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: jsonData
        })
        
        // 处理服务器响应
        if (!response.ok) {
          throw new Error('服务器响应错误')
        }
        
        const result = await response.json()
        console.log('服务器响应:', result)
        
        // 注册成功处理
        this.$router.push('/login')
        this.$store.dispatch('showToast', '注册成功，请登录！')
      } catch (error) {
        this.errorMessage = '注册失败，请稍后重试'
        console.error('注册错误:', error)
      } finally {
        this.isSubmitting = false
      }
    }
  }
}
</script>

<style scoped>
.auth-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f0f2f5;
}

.auth-form {
  background: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  width: 100%;
  max-width: 400px;
}

.auth-form h2 {
  text-align: center;
  margin-bottom: 2rem;
  color: #2c3e50;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  color: #666;
}

.form-group input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  transition: border-color 0.3s ease;
}

.form-group input:focus {
  border-color: #42b983;
  outline: none;
}

button[type="submit"] {
  width: 100%;
  padding: 1rem;
  background: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: opacity 0.3s ease;
  margin-top: 1rem;
}

button[type="submit"]:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.auth-links {
  margin-top: 1.5rem;
  text-align: center;
}

.auth-links a {
  color: #42b983;
  text-decoration: none;
  display: block;
  margin: 0.5rem 0;
}

.error-message {
  margin-top: 1rem;
  padding: 0.75rem;
  background: #ffeef0;
  color: #ff4d4f;
  border-radius: 4px;
  text-align: center;
}

@media (max-width: 480px) {
  .auth-form {
    margin: 1rem;
    padding: 1.5rem;
  }
}
</style>